package org.example.service.imp;

import org.example.service.LikeService;
import org.example.util.CommonParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class LikeServiceImp implements LikeService {
    /**             hash结构             hashKey                                 innerMapKey        innerMapValue
     * 点赞(帖子和评论 ) redis的存储结构 like-(discuss或者comment)-userid             like               (独一个  记录这个的点赞数量)
     *                                                                           like-user-userid   很多个（那些用户点了赞 ） 1表示用户点赞赞  0用户之前点赞 然后取消
     */
    private static final String LIKE_SYMBOL_DISCUSS = "like-discuss-";
    private static final String LIKE_SYMBOL_COMMENT = "like-comment-";
    private static final String LIKE_SYMBOL = "like";
    private static final String LIKE_USER_SYMBOL = "like-user-";
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    //点赞
    public void like(int type,int id,int userId){
        String key = setKey(type, id);
        redisTemplate.opsForHash().increment(key,LIKE_SYMBOL,1);
        redisTemplate.opsForHash().put(key,LIKE_USER_SYMBOL+userId,1);
    }
    //取消
    public void cancelLike(int type,int id,int useId){
        String key = setKey(type, id);
        Integer likeCount = getLikeCount(type, id);
        if (likeCount!=null &&likeCount>0){
            redisTemplate.opsForHash().increment(key,LIKE_SYMBOL,-1);
        }
        redisTemplate.opsForHash().put(key,LIKE_USER_SYMBOL+useId,0);
    }
    //当前帖子 或者 评论是否被赞过
    public boolean isLike(int type,int id){
        String key = setKey(type, id);
        Boolean aBoolean = redisTemplate.hasKey(key);
        return aBoolean;
    }
    //当前用是否赞过  判断用户是否点赞那么  这个帖子或者评论一定是被点赞过得
    public boolean currUserIsLike(int type,int id,int userId){
        String key = setKey(type, id);
        Boolean aBoolean = redisTemplate.hasKey(key);
        if (!aBoolean) return false;
        Integer isLike = (Integer) redisTemplate.opsForHash().get(key, LIKE_USER_SYMBOL + userId);
        if (isLike == null || isLike.equals(0)) return false;

        return true;
    }
    private String setKey(int type,int id){
        String key = null;
        if (type == 1){
            key = LIKE_SYMBOL_DISCUSS+id;
        }else if (type == 2){
            key = LIKE_SYMBOL_COMMENT+id;
        }
        return key;
    }
    public Integer getLikeCount(int type,int id){
        String key = setKey(type, id);
        Integer likeCount = null;
        if (redisTemplate.hasKey(key))  likeCount =   (Integer) redisTemplate.opsForHash().get(key, LIKE_SYMBOL);
        else likeCount = 0;
        return likeCount;
    }

    /**
     * redis 存储用户的帖子和评论的结构                           CommonParam.USER_PUBLISH_DISCUSS_SYMBOL     discussIds
     * CommonParam.USER_PUBLISH_STATUS+userid   》》  map 》》USER_PUBLISH_COMMENT_SYMBOL                 commentIds
     * @param userid
     * @return
     */
    //统计某一个用户发布的帖子和评论的点赞数量(目前就只有帖子和评论)
    public int statistics(int userid){
        int currUserLikeCount = 0;
        String userKey =  CommonParam.USER_PUBLISH_STATUS+userid;
        Set<Integer> discussIds = (Set<Integer>)redisTemplate.opsForHash().get(userKey, CommonParam.USER_PUBLISH_DISCUSS_SYMBOL);
        Set<Integer> commentIds = (Set<Integer>)redisTemplate.opsForHash().get(userKey,CommonParam.USER_PUBLISH_COMMENT_SYMBOL);
        //从redis中判断当前用户发布的帖子是否是否被点赞  点赞 获取点赞数量 没有下一个
        if (discussIds!=null &&discussIds.size()>0){
            for (Integer tempId:discussIds){
                if (isLike(1,tempId)){
                    //存在  获取 点赞数量
                    Integer likeCount = getLikeCount(1, tempId);
                    currUserLikeCount += likeCount;
                }
            }
        }
        //从redis中判断当前用户发布的评论是否是否被点赞  点赞 获取点赞数量 没有下一个
        if (commentIds!=null &&commentIds.size()>0){
            for (Integer tempId:commentIds){
                if (isLike(2,tempId)){
                    //存在  获取 点赞数量
                    Integer likeCount = getLikeCount(2, tempId);
                    currUserLikeCount += likeCount;
                }
            }
        }
        return currUserLikeCount;
    }
    //当用户点赞一个帖子时  这个帖子的分数需要改变 影响热贴排名 我们需要记录那些帖子(如果多人点赞一个帖子我们存储一个帖子id就行)
    //这里目的就是存放一个帖子id到redis中  目的是为了计算帖子分数
    public void saveDiscussIdToHotKey(int discussId){
        if (discussId<=0) return;
        redisTemplate.opsForSet().add(CommonParam.HOT_DISCUSS_KEY,discussId);
    }

}
